<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;
use app\common\library\Auth;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 佣金管理
 *
 * @icon fa fa-user
 */
class Walletlog extends Backend
{

    protected $relationSearch = true;
    protected $searchFields = 'id';

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\shopro\user\WalletLog;
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with(['order','orderm','orders'])
                ->where($where)
                ->where(['fa_shopro_user_wallet_log.wallet_type'=>'money','fa_shopro_user_wallet_log.type'=>'commission_income','fa_shopro_user_wallet_log.oper_type'=>'system'])
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $key => $value) {
                if ($value->ext == '{"classify":3}') {
                    $value->temp_source = '购买会员';
                    $value->temp_order_no = $value->order->order_sn;;
                } else if ($value->ext == '{"classify":2}') {
                    $value->temp_source = '五行穿衣';
                    $value->temp_order_no = $value->orders->order_id;;
                } else if ($value->ext == '{"classify":1}') {
                    $value->temp_source = $value->orderm->sn;;
                    $value->temp_order_no = '笔画查询';
                } else {
                    $value->temp_source = '无';
                    $value->temp_order_no = '';
                }
                
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }
}
